import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';

// 路由配置
final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    GoRoute(
      path: "/",
      builder: (context, state) => const HomeScreen(),
      routes: <RouteBase>[
        GoRoute(
          path: "details",
          builder: (context, state) => const DetailsScreen(),
        ),
        GoRoute(
          path: "route",
          builder: (context, state) => const DetailsScreen(),
        ),
      ],
    ),
    GoRoute(
      path: "route",
      builder: (context, state) => const HomeScreen(),
      routes: <RouteBase>[
        GoRoute(
          path: "details",
          builder: (context, state) => const DetailsScreen(),
        ),
      ],
    ),
  ],
);

/// 路由 APP
class RouteApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
    );
  }
}

/// 根路由
class HomeScreen extends StatelessWidget {
  /// Constructs a [HomeScreen]
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home Screen')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () => {SystemNavigator.pop()},
              child: const Text('退出当前Flutter应用'),
            ),
            ElevatedButton(
              onPressed: () => context.go('/details'),
              child: const Text('Go to the Details screen'),
            ),
          ],
        ),
      ),
    );
  }
}

/// 二级
class DetailsScreen extends StatelessWidget {
  /// Constructs a [DetailsScreen]
  const DetailsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Details Screen')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <ElevatedButton>[
            ElevatedButton(
              onPressed: () => context.go('/'),
              child: const Text('Go back to the Home screen'),
            ),
          ],
        ),
      ),
    );
  }
}

class FlutterApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var app = MaterialApp(
      theme: ThemeData.light(),
      // home: FlutterPage(),
      // initialRoute: window.defaultRouteName,
      routes: {
        "/": (context) => FlutterPage("/"),
        "/root": (context) => FlutterPage("root"),
        "/default": (context) => FlutterPage("default"),
        "/app": (context) => FlutterPage("app"),
      },
    );
    // MaterialApp.router(
    //   routerConfig: GoRouter,
    // );
    return app;
  }
}

class FlutterPage extends StatefulWidget {
  String title;
  FlutterPage(this.title);

  @override
  State createState() {
    return FlutterState(title);
  }
}

class FlutterState extends State<FlutterPage> {
  String title;
  FlutterState(this.title);

  static const channel = MethodChannel('flutter_bridge');

  Future<String> _channelFunc(String funcName) async {
    return await channel.invokeMethod(funcName);
  }

  String channelMessasge = "传值-> Native";

  Future<void> channelMessage() async {
    String result = "调用原生";
    try {
      result = await _channelFunc("Native");
    } on PlatformException catch (e) {
      result = "请继续点击，但每次会 +2 !";
    }
    setState(() {
      channelMessasge = result;
    });
  }

  Future<void> channelChange() async {
    try {
      await _channelFunc("Channel");
    } on PlatformException catch (e) {
      e.message;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            GestureDetector(
              child: Text(
                "返回",
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              onTap: () {
                SystemNavigator.pop();
              },
            ),
            GestureDetector(
              child: Text(
                channelMessasge,
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              onTap: () {
                channelMessage();
              },
            ),
            GestureDetector(
              child: Text(
                "切换路由",
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              onTap: () {
                channelChange();
              },
            ),
            Text(
              window.defaultRouteName,
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            Text(
              title,
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            Text(
              '这是另一个入口 twoMain:$title',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
    );
  }
}
